
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	var wind_async = require('ddf_wind_async');

	var base = require('component-base');
	
	var MaxImgCount=6;//最多允许图片数量
	var currentImgCount=0;//已上传的图片数
	
	//限制上传图片大小
	var maxSize= 2*1024*1024;//上传图片最大2M
	/**
	 * 上传
	 */
	exports.ddf_upload = function(id,path,fn) {
		$("#"+id).ddf_upload_file({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				successCallBack : fn
			}

		});
	};
	

	$.fn.ddf_upload_file = function(opts) {

			//初始化上传控件
			function init(){
				

				if($("#_upload_frame_"+opts.options.form_id).length==0){
					$(document.body).append('<iframe class="opacitystyle" style="display:none;height:0px;" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
				}
				var htmlStr="";
				htmlStr += '<form id="'+opts.options.form_id+'" method="post" enctype="multipart/form-data">';
				htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" size="19" style="opacity: 0;"><span class="filename"></span><span class="action">选择文件</span></div>';
				htmlStr += '</form>';
				$("#"+opts.options.div_id).html(htmlStr);



				
				$("#"+opts.options.input_id).change(function(){
					if(!CheckUploadFile(opts.options.input_id))
						return;
					/*$("#"+opts.options.form_id)[0].action=opts.options.url+"?path="+opts.options.path+"&rename=true";
					$("#"+opts.options.form_id)[0].target="_iframe";
					$("#"+opts.options.form_id).submit();*/
					//alert(opts.options.url+"?path="+opts.options.path+"&rename=true&callBack="+opts.options.successCallBack);"overwrite":"true"
					base.showLoading();
					var frame_url=window.location.protocol +"//" + window.location.host+CTX+"/frame.jsp?callBack="+opts.options.successCallBack;
					
					//var frame_url=window.location.protocol +"//" + window.location.host+"/"+CTX+"/admin/c.jsp";
					document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;
					//document.getElementById(opts.options.form_id).action="http://localhost:8080/file/b.jsp";

					//document.getElementById(opts.options.form_id).action="http://localhost:8080/file/frame.jsp";
					document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
					//alert(2);
//					document.getElementById(opts.options.form_id).target="_blank";
					document.getElementById(opts.options.form_id).submit();
					
					//alert(3);
					 /*
					$("#"+opts.options.form_id).ajaxSubmit({
	                    type: 'post',
	                    url: opts.options.url+"?path="+opts.options.path+"&rename=true",
	                    //contentType:"text/plain;charset=UTF-8",     
	                    dataType:'script',
	                    success: function(data){
	                    	alert(data);
	                    	if(data["code"]==1){
	                    		
	                    		 opts.options.successCallBack(data["files"][0]["path"]);
	                    		
	                    	}else{
	                    		
	                    		 base.tips_error("上传失败:"+data["msg"]);
	                    	}
	                       
	                    },
	                    error: function(XmlHttpRequest, textStatus, errorThrown){
	                    	
	                       base.tips_error("error:上传失败");
	                    }
	                });
					*/
					
					
				});
				
			}
			
			init();
	};
	
	function CheckUploadFile(id) {
		
		var tmp = document.getElementById(id).value;
		if (tmp != '') {
			tmp = tmp.toLowerCase();
			var patten = /(.jpg|.png|.gif)$/;
			if (tmp.search(patten) <= -1) {
				base.tips_error('只允许上传 jpeg/gif/png 格式的文件');
				return false;
			}
		}

		return true;
	}
	
	/**
	 * 上传商家相册
	 */
	exports.ddf_upload_shopPhots = function(id,path,fn,allowNum) {
		$("#"+id).ddf_upload_image({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				allowNum : allowNum,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ddf_upload_image = function(opts) {

		//初始化上传控件
		function init(){
			

			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe class="opacitystyle" style="display:none;height:0px;" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form id="'+opts.options.form_id+'" style="display:none" method="post" enctype="multipart/form-data">';
			htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" multiple="multiple" size="19" style="opacity: 0;"><span class="filename"></span><span class="action">选择文件</span></div>';
			htmlStr += '</form>';
			$("#divLink"+opts.options.div_id).html(htmlStr);
			
			$("#"+opts.options.input_id).click();	
			$("#"+opts.options.input_id).change(function(){
				if(!CheckUploadFile(opts.options.input_id))
					return;
				if(!fileCountCheck(opts.options.input_id,opts.options.allowNum,"image"))
					return;
				base.showLoading();
				var frame_url=window.location.protocol +"//" + window.location.host+CTX+"/frame.jsp?callBack="+opts.options.successCallBack;
				
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;

				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				
				document.getElementById(opts.options.form_id).submit();
				
				
			});
			
		}
		
		init();
	};
	
	/**
	 * 上传取消原因图片
	 */
	exports.ddf_upload_reasonPhots = function(id,path,fn,allowNum) {
		$("#"+id).ddf_upload_reason({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				allowNum : allowNum,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ddf_upload_reason = function(opts) {

		//初始化上传控件
		function init(){
			
			
			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe class="opacitystyle" style="display:none;height:0px;" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form id="'+opts.options.form_id+'" method="post" enctype="multipart/form-data">';
			htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" size="19" style="opacity: 0;" multiple="multiple"><span class="filename"></span><span class="action">选择文件</span></div>';
			htmlStr += '</form>';
			$("#"+opts.options.div_id).html(htmlStr);
			$("#"+opts.options.input_id).click();	
			$("#"+opts.options.input_id).change(function(){
				if(!CheckUploadFile(opts.options.input_id))
					return;
				if(!fileCountCheck(opts.options.input_id,opts.options.allowNum,"reason"))
					return;
				
				base.showLoading();
				var frame_url=window.location.protocol +"//" + window.location.host+CTX+"/frame.jsp?callBack="+opts.options.successCallBack;
				
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;

				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				
				document.getElementById(opts.options.form_id).submit();
				
				
			});
			
		}
		
		init();
	};
	
	function fileCountCheck(_id,_allowNum,showType){ 
		var num=0;
		$.each($('#'+_id).prop("files"), function(k, v) {
		    num++;
		});
		
		if(num>_allowNum){
			if(showType=="merchandise"){
				base.tips_error('商品图片的数量不能超过6张');
			}else{
				base.tips_error('您上传的文件总个数已经超过允许上传总个数！');
			}
			return false;
		}
		return true;                     
	}
	
	
	/**
	 * 上传商品图片
	 */
	exports.ddf_upload_merchandiseImg = function(id,path,fn) {
		$("#"+id).ddf_upload_merImg({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ddf_upload_merImg = function(opts) {

		//初始化上传控件
		function init(){

			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe class="opacitystyle" style="display:none;height:0px;" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form id="'+opts.options.form_id+'" method="post" enctype="multipart/form-data">';
			htmlStr += '<img id="preview'+opts.options.form_id+'"/>';
			htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" size="19" multiple="multiple"  style="opacity: 0;"><span id="filename'+opts.options.input_id+'" class="filename"></span><span class="action">选择文件</span></div>';
			htmlStr += '</form>';
			$("#"+opts.options.div_id).html(htmlStr);
			
			
			$("#filename"+opts.options.input_id).click(function(){
				$("#"+opts.options.input_id).val("");
			});
			
			$("#"+opts.options.input_id).change(function(){
				var textVal = $("#"+opts.options.input_id).val();
				if(isNull(textVal)){
					//当用户取消上传时
					return;
				}

				if(!CheckUploadFile(opts.options.input_id))
					return;
				
				var allow=MaxImgCount-currentImgCount;
				if(!fileCountCheck(opts.options.input_id,allow,"merchandise"))
					return;
//				if(!onUploadImgChange(this))
//					return;
				
				base.showLoading();
				var frame_url=window.location.protocol +"//" + window.location.host+CTX+"/frame.jsp?callBack="+opts.options.successCallBack;
				
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;
				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				document.getElementById(opts.options.form_id).submit();
				
				
			});
			
			
		}
		
		init();
	};
	
	
	
	function onUploadImgChange(sender,id){
		  var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
		  var fileSize = 0;
//		  var objPreview = document.getElementById( 'preview' );
//		  var objPreviewFake = document.getElementById( 'preview_fake' );
//		  var objPreviewSizeFake = document.getElementById( 'preview_size_fake' );
		  try{
			  
			  
			  if(isIE && !sender.files){
//				  alert("IE");
//				  sender.select();
//				    var imgSrc = document.selection.createRange().text;
//				    
//				    objPreviewFake.filters.item(
//				     'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
//				    objPreviewSizeFake.filters.item(
//				     'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
//				    
//				    autoSizePreview( objPreviewFake, 
//				     objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );
//				    objPreview.style.display = 'none';
//				    //读取图片文件大小        
//				    var sh = setInterval(
//				    function(){
//				    var img = document.createElement("img");
//				    img.src = imgSrc;
//				    fileSize = img.fileSize;
//				    if (fileSize > 0){ checkFileSize(sender,fileSize);clearInterval(sh);}
//				    img = null;
//				    }
//				    ,100);
				  return true;
			  }else{
				  fileSize = sender.files[0].size;
				  return checkFileSize(sender,fileSize);
				  
			  }
		  }catch(e)
		  {
			  base.tips_error("您上传的文件过大或者不是图片,请重新上传！");
		  }    
		 }
		 
		 function checkFileSize(sender,fileSize)
		 {
		     if(fileSize > maxSize)
		     {
		    	 base.tips_error("图片大小超过1M，请缩小后上传!");
		         return false;
		     }
			 
		     return true;
		     
		 }
		 
		 
		 //设置已上传图片数量
		 exports.setCurrentMerCount=function(index){
			 currentImgCount=index;
		 };
		 
		//获取已上传图片数量
		 exports.getCurrentMerCount=function(index){
			 return currentImgCount;
		 };
	     
		 
	
});